/*
 * Database Configuration for Staging Process
 *
 *
 */

$(document).ready(function(){
   
    /*  ==========================================================
        =====  Load list of tables on selected database name =====
        ========================================================== */
    $("body").on("change","#select_database",function(){
        var db_name = $("#select_database option:selected").text();
        var action = "get_table";
        
        $(".export_loader").show();
        var ret = '';
        $.ajax({
            type  :  "POST",
            url   :  "/ajax/database_configuration",
            data  :  {action:action,db_name:db_name},
            dataType : "json",
            success : function(data){
                var data_length = data.length;
                for(var i = 0; i < data_length; i++){
                    var list_table_name = data[i]['Tables_in_' + db_name];
                    $(".get_selected_tables").show();
                    $(".put_table_name").html(db_name);
                    ret += '<option value="' + i + '">' + list_table_name + '</option>';
                }
                $(".table_list").html(ret);
                $(".export_loader").hide();
                console.log(data);
                /* Multiselect */
                    $('.table_list').multiSelect('refresh');
            }
        });
    });
    
    /*  ================================================
        =====  Generate Exporting (Save Functions) =====
        ================================================ */
    $("body").on("click",".generate_exporting",function(){
        
        $(".export_loader").show();
        var get_table_list_selected = $(".table_list").attr("id");
        var tb = $("#ms-" + get_table_list_selected).find(".ms-selection").find('ul.ms-list');
   
        var database_name = $("#select_database option:selected").text();
        var tables = $("#ms-" + get_table_list_selected + " .ms-selection .ms-list .ms-selected").map(function(){
            return $(this).text();    
        }).get().join();
        var action = "generate_exporting";
  
        if (tables == "") {
            notify_e('Error','Please select atleast one table name.');
            $(".export_loader").hide();
        }else{
            $.ajax({
                type : "POST",
                url  : "/ajax/database_configuration",
                data : {action:action,database_name:database_name,tables:tables},
                success : function(e){
                    if (e == "Selected database was successfully exported.") {
                       
                        $(".export_loader").hide();
                        $('#fmultiselect').multiSelect('deselect_all');
                        notify_s('Success',database_name + ' was successfully exported.');
                    }
                }
            });
        }
        
    });
    
    
    /*  ===================================================
        =====  Change database connection information =====
        =================================================== */
    $("body").on("click",".update_database_configuration",function(event){
        $("#validate").validationEngine({promptPosition : "topLeft", scroll: true});
        // this will prevent the form from submitting 
            event.preventDefault();
        
        // Validate required fields
            var formvalidate;
            formvalidate = $("#validate").validationEngine("validate",{promptPosition : "topLeft", scroll: true});
                if(formvalidate == true) {
                    var myJSON = {}
                    $(".config").each(function(){
                        var attribute_name = $(this).attr("name");
                        var config_value = $(this).val();
                        
                        myJSON[attribute_name] = config_value;
                         
                    });
                    var jsonStringify = JSON.stringify(myJSON);
                    console.log(jsonStringify)
                }
                
            
        
    });
    
});

